//
//  DDDetailsTitleView.m
//  Dingdong
//
//  Created by 程强 on 16/11/17.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDDetailsTitleView.h"
#import "ImageScrollView.h"

@interface DDDetailsTitleView()
@property (strong, nonatomic) ImageScrollView *imageScrollView;
@property (strong, nonatomic) KKLabel *nameLbl;
@property (strong, nonatomic) KKLabel *priceLbl;
@property (strong, nonatomic) KKLabel *volumeLbl;

@end

@implementation DDDetailsTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = RGBColor(237, 237, 237);
        UIView *topView = [[UIView alloc]init];
        [self addSubview:topView];
        topView.backgroundColor = [UIColor redColor];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.centerX.equalTo(self);
            make.width.equalTo(self);
            make.height.equalTo(topView.mas_width).multipliedBy(1.0);
        }];
        
        self.imageScrollView = [[ImageScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth)];
        [topView addSubview:_imageScrollView];
        _imageScrollView.placePics = @[];
        _imageScrollView.pics = @[];
        [_imageScrollView reloadView];
        _imageScrollView.backgroundColor = [UIColor whiteColor];
        [_imageScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(topView);
        }];
        [_imageScrollView returnIndex:^(NSInteger index) {
            if (self.previewBlock) {
                self.previewBlock(index,self.galleryAry);
            }
        }];
        
        UIView *midView = [[UIView alloc]init];
        [self addSubview:midView];
        midView.backgroundColor = [UIColor whiteColor];
        [midView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.width.bottom.equalTo(self);
            make.top.equalTo(topView.mas_bottom).offset(1.0);
        }];
        
        self.nameLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",[UIColor blackColor],[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        _nameLbl.numberOfLines = 0;
        [midView addSubview:_nameLbl];
        [_nameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(midView);
            make.width.equalTo(midView).offset(-20);
            make.top.equalTo(midView).offset(3.0);
            make.height.equalTo(@40);
        }];
        
        self.priceLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",kRedColor,[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        [midView addSubview:_priceLbl];
        [_priceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(midView).offset(10);
            make.top.equalTo(_nameLbl.mas_bottom);
            make.width.equalTo(@120);
            make.height.equalTo(@30);
        }];
        
        self.volumeLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",[UIColor darkGrayColor],[UIFont systemFontOfSize:13.0],NSTextAlignmentLeft);
        }];
        [midView addSubview:_volumeLbl];
        [_volumeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(midView).offset(10);
            make.top.equalTo(_priceLbl.mas_bottom);
            make.width.equalTo(@120);
            make.height.equalTo(@30);
        }];
        _volumeLbl.hidden = YES;
    }
    return self;
}
- (void)setGoodsName:(NSString *)goodsName
{
    _goodsName = goodsName;
    self.nameLbl.text = goodsName;
    
    CGSize size = [ToolKit getSizeByFont:[UIFont systemFontOfSize:15.0] labelWidth:ScreenWidth-20 message:goodsName];
    if (size.height>40) {
        [_nameLbl mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(size.height+5));
        }];
    }
    if (self.updateBlock) {
        self.updateBlock(size.height>40?size.height+5:40);
    }
}
- (void)setGoodsPrice:(NSString *)goodsPrice
{
    _goodsPrice = goodsPrice;
    self.priceLbl.text = [NSString stringWithFormat:@"$%@",goodsPrice];
}
- (void)setGoodsVolume:(NSString *)goodsVolume
{
    _goodsVolume = goodsVolume;
    self.volumeLbl.text = [NSString stringWithFormat:@"volume:%@",goodsVolume];
}
- (void)setGalleryAry:(NSArray *)galleryAry
{
    _galleryAry = galleryAry;
    self.imageScrollView.pics = galleryAry;
    self.imageScrollView.placePics = galleryAry;
    [self.imageScrollView reloadView];
}
- (void)updateDetailTitleViewHeight:(TitleFrameUpdate)block
{
    self.updateBlock = block;
}
- (void)userPreviewLargePhotos:(PreviewLargePic)block
{
    self.previewBlock = block;
}
@end
